package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.CheckGroupException;
import com.itheima.pojo.CheckGroup;
import com.itheima.vo.CheckGroupVo;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass =CheckGroupService.class )
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;


    @Override
    public void add(CheckGroup checkGroup) {
        //保存checkGroup并返回住建
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();
        setCheckItemAndCheckGroupBatch(checkGroupId, checkitemIds);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroups=checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),checkGroups);
    }

    @Override
    public CheckGroupVo findDetailForUpdate(Integer id) {
        //获取检查组详情
       CheckGroup checkGroup= checkGroupDao.findById(id);
        //获取所有检查项
        List<CheckItem> checkItems=checkItemDao.findAll();
        //获取检查组已经勾选的检查项id
        List<Integer> checkItemIds=checkGroupDao.findCheckItemIdsCheckGroupById(id);
        CheckGroupVo checkGroupVo=new CheckGroupVo(checkItems,checkGroup,checkItemIds);
        return checkGroupVo;
    }

    @Override
    public void edit(CheckGroup checkGroup) {
        //修改检查组的基本信息
        checkGroupDao.edit(checkGroup);
        //删除检查组和检查项的老的关系
        checkGroupDao.deleteAssocation(checkGroup.getId());
        //插入检查组和检查项新的关系
        setCheckItemAndCheckGroupBatch(checkGroup.getId(),checkGroup.getCheckitemIds());
    }

    @Override
    public void delete(Integer id) {
       Integer count= checkGroupDao.findCountByCheckGroupId(id);
       if (count!=null && count>0){
          //存在引用提示用户
           throw new CheckGroupException("存在引用不能删除！");
       }
        checkGroupDao.deleteAssocation(id);
        checkGroupDao.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups=checkGroupDao.findAll();
        return checkGroups;
    }

    private void setCheckItemAndCheckGroupBatch(Integer checkGroupId, List<Integer> checkitemIds) {
        //循环插入检查组和检查项的关系（批量插入）
        if (null!=checkitemIds&&checkitemIds.size()>0){
            //批量插入
            List<Map> batchParam= new ArrayList<>();
            for (Integer checkitemId : checkitemIds) {
                Map map=new HashMap();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                batchParam.add(map);
            }
            checkGroupDao.setCheckItemAndCheckGroupBatch(batchParam);
        }
    }

}
